//
//  BasNavViewController.swift
//  Weibo
//
//  Created by 潇 on 16/1/3.
//  Copyright © 2016年 潇. All rights reserved.
//

import UIKit

class BasNavViewController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        //设置返回手势的代理
        self.interactivePopGestureRecognizer?.delegate = self
    }

    override func pushViewController(viewController: UIViewController, animated: Bool) {
        
        //viewController是将要跳转的页面
        //自定义返回按钮
        //一旦自定义返回按钮 导航时空控制器的返回手势的协议对象会被重置
        if childViewControllers.count != 0 {
        
            let btn = UIButton(title: "返回", backImageName:  nil, fontSize: 14, color: UIColor.lightGrayColor(), imageName: "navigationbar_back_withtext")
            btn.addTarget(self, action: "back", forControlEvents: .TouchUpInside)
            
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: btn)
            
            viewController.hidesBottomBarWhenPushed = true
        }
        
        //调用父类的跳转函数
        super.pushViewController(viewController, animated: true)
        
    }
    
    @objc private func back() {
        self.popViewControllerAnimated(true)
    }

    //返回手势滑动
    func gestureRecognizerShouldBegin(gestureRecognizer: UIGestureRecognizer) -> Bool {
        
//        if childViewControllers.count == 1 {
//            return false
//        }
//        
//        return true
        return childViewControllers.count != 1
    }

   

}
